package com.huopaolan.yohuoshop.Mvc.Model;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

public class LetterBar extends View {
//    //popu
//    private TextView popu;
//    private PopupWindow popupWindow;
//    private View view;
    //画笔
    private Paint paint = new Paint();
    //选中的字母索引
    private int index = -1;
    //选中的字体颜色
    private int chooseColor = Color.MAGENTA;
    //字母默认颜色
    private int defautColor = Color.BLACK;
    //选中背景颜色
    private int chooseBackgroundColor = Color.LTGRAY;
    //是否触摸
    private boolean isTouch;
    //字母大小
    private int textSize = 15;
    //字母监听
    private OnTouchLetterChangeListener onTouchLetterChangeListener;
    //字母数组
    private static String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private STYLE chooseStyle = STYLE.DEFAULT;

    //选中时背景风格
    public enum STYLE {
        DEFAULT, NONE, CIRCLE, STRETCH
    }

    public LetterBar(Context context, AttributeSet attrs) {
        super(context, attrs);
//        popu = new TextView(context);
//        popu.setTextColor(Color.WHITE);
//        popu.setTextSize(25);
//        popu.setBackgroundColor(Color.GREEN);
//        popu.setGravity(Gravity.CENTER);
//        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
//        );
//        popu.setLayoutParams(layoutParams);
//        popupWindow = new PopupWindow(popu, 150, 150);
//        AppCompatActivity appCompatActivity = (AppCompatActivity) context;
//        view = appCompatActivity.getWindow().getDecorView().getRootView();
    }

    public LetterBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //TODO 设置画笔颜色
    public void setDefautColor(int defautColor) {
        this.defautColor = defautColor;
    }

    //TODO 设置背景颜色
    public void setChooseBackgroundColor(int chooseBackgroundColor) {
        this.chooseBackgroundColor = chooseBackgroundColor;
    }

    //TODO 设置字体大小
    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    //TODO 设置选中控件背景
    public void setChooseStyle(STYLE chooseStyle) {
        this.chooseStyle = chooseStyle;
    }

    //TODO 接口回调事件
    public void setOnTouchLetterChangeListener(OnTouchLetterChangeListener onTouchLetterChangeListener) {
        this.onTouchLetterChangeListener = onTouchLetterChangeListener;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        //字母个数
        int length = letters.length;
        //单个字母的高度
        int singHeight = height / length;
        //触摸时画出背景色
        if (isTouch && chooseBackgroundColor != Color.TRANSPARENT && chooseStyle != STYLE.NONE) {
            paint.setAntiAlias(true);
            paint.setColor(chooseBackgroundColor);
            //选中圆形背景效果
            if (chooseStyle == STYLE.CIRCLE) {
                int maxVaule = Math.max(width, singHeight);
                int minVaule = Math.min(width, singHeight);
                canvas.drawArc(new RectF((maxVaule - minVaule) / 2,
                                singHeight * index,
                                singHeight + (maxVaule - minVaule) / 2,
                                singHeight * index + singHeight),
                        0, 360, true, paint);
                //选中背景拉伸效果
            } else if (chooseStyle == STYLE.STRETCH) {
                canvas.drawArc(new RectF(0, 0, width, singHeight * index), 0, 360, true, paint);
            } else {
                canvas.drawArc(new RectF(0, 0, width, singHeight), 180, 180, true, paint);
                canvas.drawRect(new RectF(0, singHeight / 2, width, height - singHeight / 2), paint);
                canvas.drawArc(new RectF(0, height - singHeight, width, height), 0, 180, true, paint);
            }
        }
        //画字母
        for (int i = 0; i < length; i++) {
            //设置字体格式
            paint.setTypeface(Typeface.DEFAULT);
            paint.setAntiAlias(true);
            //设置字体大小
            paint.setTextSize(30);
            if (i == index) {//选中时画笔颜色
                paint.setColor(chooseColor);
            } else {
                paint.setColor(defautColor);
            }
            //触摸时
            if (isTouch) {
                paint.setFakeBoldText(true);
            } else {
                paint.setFakeBoldText(false);
            }
            //字母坐标
            float x = width / 3;
            float y = (singHeight * (i + 1)) - (paint.measureText(letters[i]) / 2);
            //画字母
            canvas.drawText(letters[i], x, y, paint);
            //重置
            paint.reset();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //当前字母的索引
        int index = (int) (event.getY() / getHeight() * letters.length);
        //旧索引
        int oldIndex = this.index;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouch = true;
                if (index != oldIndex && index >= 0 && index < letters.length) {
                    this.index = index;
                    if (onTouchLetterChangeListener != null) {
//                        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
//                        popu.setText(letters[index]);
                        Log.e("", "111111111111111" + letters[index]);
                        onTouchLetterChangeListener.onTouchLetterChange(isTouch, letters[index]);
                    }
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                isTouch = true;
                if (index != oldIndex && index >= 0 && index < letters.length) {
                    this.index = index;
                    if (onTouchLetterChangeListener != null) {
//                        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
//                        popu.setText(letters[index]);
                        Log.e("111111111111111", "" + letters[index]);
                        onTouchLetterChangeListener.onTouchLetterChange(isTouch, letters[index]);
                    }
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                isTouch = false;
                if (index >= 0 && index < letters.length) {
                    this.index = index;
                    if (onTouchLetterChangeListener != null) {
                        onTouchLetterChangeListener.onTouchLetterChange(isTouch, letters[index]);
                    }
                }
                this.index = -1;
                invalidate();
//                popupWindow.dismiss();
                break;
            default:
                break;
        }
        return true;
    }

    //TODO 字母改变接口
    public interface OnTouchLetterChangeListener {
        void onTouchLetterChange(boolean isTouch, String letter);
    }
}
